<?php

/**
 * goods_class
 * @version 1.0
 * @package application
 * @subpackage application/controllers/goods_class/
 */
class Goods_class extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->_rule_config = array(
//            array('field' => 'gc_id', 'label' => '索引ID', 'rules' => 'required'),
            array('field' => 'gc_name', 'label' => '分类名称', 'rules' => 'required'),
//            array('field' => 'type_id', 'label' => '类型id', 'rules' => 'required'),
//            array('field' => 'type_name', 'label' => '类型名称', 'rules' => 'required'),
//            array('field' => 'gc_parent_id', 'label' => '父ID', 'rules' => 'required'),
//            array('field' => 'gc_sort', 'label' => '排序', 'rules' => 'required'),
//            array('field' => 'gc_show', 'label' => '前台显示，0为否，1为是，默认为1', 'rules' => 'required'),
//            array('field' => 'gc_index_show', 'label' => '首页显示 1：默认 显示 0：不显示', 'rules' => 'required'),
//            array('field' => 'gc_keywords', 'label' => '关键词', 'rules' => 'required'),
//            array('field' => 'gc_description', 'label' => '描述', 'rules' => 'required'),
//            array('field' => 'created', 'label' => '添加时间', 'rules' => 'required'),
//            array('field' => 'modified', 'label' => '修改时间', 'rules' => 'required'),
//            array('field' => 'status', 'label' => '状态', 'rules' => 'required'),
        );

        $this->load->model('goods_class_model');
    }

    /**
     * index 显示列表页
     * @author 
     */
    public function index() {
        $attributes = $this->input->get();
        $search['attributes'] = $attributes;
        // 总数与分页
        $total = $this->goods_class_model->total($search);
        $data['total'] = $total;
        // 请使用config_item( 'per_page' )获取全局显示条数
        $per_page = config_item('per_page');
        $this->load->library('pagination');
        $pagination_config = array(
            'base_url' => base_url($this->_site_path . '/goods_class/index'),
            'total_rows' => $total,
            'per_page' => $per_page,
            'uri_segment' => 4,
        );
        $this->pagination->initialize($pagination_config);
        $data['pagination'] = $this->pagination->create_links();

        $search['limit'] = array('persize' => $per_page, 'offset' => $this->pagination->get_cur_offset());
        $data['goods_class_data'] = $this->goods_class_model->all($search);
        $this->load->view($this->_site_path . '/goods_class/index', $data);
    }

    /**
     * view 显示详细页
     * @param $id 主键值
     * @author 
     */
    public function view($id) {
        $data['goods_class_data'] = $this->goods_class_model->get_by_pk($id);
        $this->load->view($this->_site_path . '/goods_class/view', $data);
    }

    /**
     * add 添加 
     * @author 
     */
    public function add() {
        $data = array();

        if ($this->input->post('is_submit') && $this->form_validation->check_token()) {
            try {
                $goods_class_data = $this->input->post();
                if ($goods_class_data['flag'] == 0) throw new Exception($goods_class_data['gc_url'], 0);
                unset($goods_class_data['flag']);
                $data['goods_class_data'] = $goods_class_data;
                //设置验证规则
                $this->form_validation->set_rules($this->_rule_config);
                if (!$this->form_validation->run()) throw new Exception(validation_errors(), 0);
                //插入
                $goods_class_id = $this->goods_class_model->insert($goods_class_data);
                init_messagebox('添加成功', 'success', 1, base_url($this->_site_path . '/goods_class/index'));
            } catch (Exception $e) {
                init_messagebox($e->getMessage(), 'error', $e->getCode());
            }
        }
        $this->load->view($this->_site_path . '/goods_class/form', $data);
    }

    /**
     * edit 编辑 
     * @param $id 主键值
     * @author 
     */
    public function edit($id) {
        $data = array();
        $data['goods_class_data'] = $this->goods_class_model->get_by_pk($id);
        if ($this->input->post('is_submit') && $this->form_validation->check_token()) {
            try {
                $goods_class_data = $this->input->post();
                if ($goods_class_data['flag'] == 0) throw new Exception($goods_class_data['gc_url'], 0);
                unset($goods_class_data['flag']);
                $data['goods_class_data'] = $goods_class_data;
                //设置验证规则
                $this->form_validation->set_rules($this->_rule_config);
                if (!$this->form_validation->run()) throw new Exception(validation_errors(), 0);
                //修改
                $this->goods_class_model->update_by_pk($goods_class_data, $id);
                init_messagebox('修改成功', 'success', 1, base_url($this->_site_path . '/goods_class/index'));
            } catch (Exception $e) {
                init_messagebox($e->getMessage(), 'error', $e->getCode());
            }
        }
        $this->load->view($this->_site_path . '/goods_class/form', $data);
    }

    /**
     * 获取插入到数据库中的通用数据
     * @param  [array] [post提交的数据]
     * @return [null]
     */
    private function getInsertData($news_data) {
        if ($_FILES['gc_url']['name'] != '') {
            $result = uploadFile('gc_url');
            $news_data['flag'] = $result['flag'];
            $news_data['gc_url'] = $result['msg'];
        } else {
            $news_data['flag'] = 2;
            $news_data['gc_url'] = "";
        }
        return $news_data;
    }

    /**
     * delete 删除
     * @param $id 主键值
     * @author 
     */
    public function delete($id) {
        $this->goods_class_model->delete_by_pk($id);
        redirect(base_url($this->_site_path . '/goods_class/index'));
    }

    /**
     * delete_all 批量删除
     * @param $id 主键值
     * @author 
     */
    public function delete_all() {
        $ids = $this->input->post('ids');
        foreach ($ids as $id) {
            $this->goods_class_model->delete_by_pk($id);
        }
        redirect(base_url($this->_site_path . '/goods_class/index'));
    }

}
